include("shared.lua")
--require("renderx")

ENT.DomesInPlay = {}
ENT.ClipPlanes = {}

function ENT:Initialize()
	--while (self.Entity:GetNetworkedString("domematerial") == nil) or (self.Entity:GetNetworkedString("domematerial") == "") do end
	self.mat = Material(self.Entity:GetNetworkedString("domematerial"))
	self.Entity:SetModel("models/zup/shields/200_shield.mdl")
	self.Radius = self.Entity:GetNetworkedInt("Radius")
	local ModelSize = (self.Entity:OBBMaxs() - self.Entity:OBBCenter()).z
	self.ModelScale = self.Radius/ModelSize
	self.FormOrigin = self.Entity:GetNetworkedString( "projectorigin" )
	self.FormTime = 3.14
	self.FormColorVec = self.Entity:GetNetworkedVector( "impactcolor" )
	self.FormColor = Color(self.FormColorVec.x, self.FormColorVec.y, self.FormColorVec.z, 255)
	self.FormMat = Material("models/shiny")
	--self.Entity:SetPos((data:GetEntity():GetPos() - self.Entity:LocalToWorld(self.Entity:OBBCenter() * self.ModelScale)) + data:GetEntity():GetPos())
	
	local boundlimit = Vector(32768,32768,32768)
	self.Entity:SetRenderBoundsWS(-1*boundlimit,boundlimit)
end

function ENT:ResyncNetVars()
	self.mat = Material(self.Entity:GetNetworkedString("domematerial"))
	self.Radius = self.Entity:GetNetworkedInt("Radius")
	local ModelSize = (self.Entity:OBBMaxs() - self.Entity:OBBCenter()).z
	self.ModelScale = self.Radius/ModelSize
	self.CreateTime = CurTime()
	self.FormColorVec = self.Entity:GetNetworkedVector( "impactcolor" )
	self.FormColor = Color(self.FormColorVec.x, self.FormColorVec.y, self.FormColorVec.z, 255)
	self.FormOrigin = self.Entity:GetNetworkedString( "projectorigin" )
end

function ENT:Draw() 
	local size = self.ModelScale
	local clip = true
	if (self.mat:GetName() != "___error") and ((self.FormOrigin == "above") or (self.FormOrigin == "below")) then
		if self.Forming == nil then
			self.Forming = true
			self.CreateTime = CurTime()
		end
		self.Entity:SetModel("models/zup/shields/200_shield.mdl")
		self.Entity:SetModelScale(Vector()*size)
		self.Entity:SetColor(self.FormColor.r, self.FormColor.g, self.FormColor.b, 240 )
		if self.Forming then
			SetMaterialOverride(self.FormMat)
			local modradius = self.Radius
			local timeprogress = math.Clamp(CurTime() - self.CreateTime, 0, self.FormTime)
			local edgedistance = ((modradius * 2) * ((self.FormTime - timeprogress) / self.FormTime)) - modradius
			local clipplane1, clipplane2, clipplane3
			if self.FormOrigin == "above" then
				clipplane1 = Plane(self.Entity:GetUp(), self.Entity:LocalToWorld(self.Entity:GetUp() * (edgedistance - (self.Radius / 10))))
				clipplane2 = Plane(self.Entity:GetUp() * -1, self.Entity:LocalToWorld(self.Entity:GetUp() * edgedistance))
				clipplane3 = Plane(self.Entity:GetUp(), self.Entity:LocalToWorld(self.Entity:GetUp() * edgedistance))
			elseif self.FormOrigin == "below" then
				local Down = self.Entity:GetUp() * -1
				clipplane1 = Plane(Down, self.Entity:LocalToWorld(Down * (edgedistance - (self.Radius / 10))))
				clipplane2 = Plane(self.Entity:GetUp(), self.Entity:LocalToWorld(Down * edgedistance))
				clipplane3 = Plane(Down, self.Entity:LocalToWorld(Down * edgedistance))
			end
			render.EnableClipping(true)
			render.PushCustomClipPlane(clipplane1.Normal, clipplane1.Distance)
			render.PushCustomClipPlane(clipplane2.Normal, clipplane2.Distance)
			local preplanes = 0
			for _,plane in pairs(self.ClipPlanes) do
				render.PushCustomClipPlane( plane.Normal, plane.Distance)
				preplanes = preplanes + 1
			end
			self.Entity:DrawModel()
			for remover=0, preplanes do
				render.PopCustomClipPlane()
			end
			table.insert(self.ClipPlanes, clipplane3)
			render.PopCustomClipPlane()
			render.PopCustomClipPlane()
			render.EnableClipping(false)
			SetMaterialOverride()
		end
		--self.Entity:SetColor(255,255,255,70)
		SetMaterialOverride(self.mat)
		self.Entity:SetMaterial(self.mat)
		local planes = 0
		if (clip) then
			render.EnableClipping(true)
			for _,plane in pairs(self.ClipPlanes) do
				render.PushCustomClipPlane( plane.Normal, plane.Distance)
				planes = planes + 1
			end
		end
		self.Entity:DrawModel()
		for remover=0, planes do
			render.PopCustomClipPlane()
		end
		render.EnableClipping(false)
		SetMaterialOverride(nil)
	end
end

function ENT:GetRadius()
	self.Entity:GetNetworkedInt("radius")
end

function ENT:Think()
	local newclipplanes = {}
	local clip = true  --util.tobool(GetConVar("cl_cdamage_shieldclippingenabled"))
	if self.Forming and (CurTime() > (self.CreateTime + self.FormTime)) then
		self.Forming = false
	end
	if (clip) then
		for k,v in pairs(self.DomesInPlay) do
			if !(v == self.Entity) then
				local result = CDamage_shieldoverlapdistance(self.Entity, v)
				if (util.tobool(result)) then
					local newplane, intersectvector = CDamage_shieldoverlapplane(self.Entity, v, result)
					table.insert(newclipplanes, Plane(intersectvector*-1, newplane + self.Entity:GetPos()))
				end
			end
		end
	end
	self.ClipPlanes = newclipplanes
	
	if (self.mat:GetName() == "___error") then
		self.Entity:ResyncNetVars()
	end
	
	self.Entity:NextThink(CurTime() + 0.1)
	return true
end

function ENT:UpdateShields(shields)
	self.DomesInPlay = shields
end